package metrics

import (
	"gitee.com/captials-team/ubdframe/src/domain/configstc"
	v1log "gitee.com/captials-team/ubdframe/src/pkg/logs"
	"github.com/prometheus/client_golang/prometheus/push"
	"os"
	"time"
)

type PrometheusPushHelper struct {
	Pusher *push.Pusher
	l      v1log.ILog

	conf configstc.MonitorReportConfig
}

// NewPrometheusPushHelper 指标上报器-prometheus
func NewPrometheusPushHelper(conf configstc.MonitorReportConfig) *PrometheusPushHelper {
	st := &PrometheusPushHelper{
		l:      v1log.NewWriterLog(os.Stdout, v1log.DebugLog),
		Pusher: push.New(conf.Host, conf.Name),
		conf:   conf,
	}
	go st.bgReport()
	return st
}

func (ind *PrometheusPushHelper) bgReport() {
	ti := time.NewTicker(time.Minute)
	//ti := time.NewTicker(time.Second * 5)
	for {
		select {
		case <-ti.C:
			err := ind.Pusher.Push()
			if err != nil {
				ind.l.Error("Could not push completion time to Pushgateway: %s", err)
			} else {
				ind.l.Info("Push success")
			}
		}
	}
}
